package com.findyou.headline.dao.impl;

import com.findyou.headline.dao.BaseDao;
import com.findyou.headline.dao.NewsUserDao;
import com.findyou.headline.pojo.NewsUser;

import java.util.List;

public class NewsUserDaoImpl extends BaseDao implements NewsUserDao {
    // 写sql语句 这个就是对数据库的查询

    @Override
    public NewsUser findByUsername(String username) {
        String sql = """
                select 
                    uid,
                    username,
                    user_pwd userPwd,
                    nick_name nickName
                from 
                     news_user 
                where
                      username = ?
                 """;
        List<NewsUser> newsUserList = baseQuery(NewsUser.class, sql, username);
        return newsUserList != null && newsUserList.size() > 0 ? newsUserList.get(0) : null;
    }

    @Override
    public NewsUser findByUid(Integer userId) {
        String sql = """
                select 
                    uid,
                    username,
                    user_pwd userPwd,
                    nick_name nickName
                from 
                     news_user 
                where
                      uid = ?
                """;
        List<NewsUser> newsUserList = baseQuery(NewsUser.class, sql, userId);
        return newsUserList != null && newsUserList.size() > 0 ? newsUserList.get(0) : null;
    }

    @Override
    public Integer insertUser(NewsUser registUser) {
        String sql = """
                insert  into news_user values(DEFAULT, ?, ?, ?);
                """;
        int rows = baseUpdate(sql, registUser.getUsername(), registUser.getUserPwd(), registUser.getNickName());
//        System.out.println(rows);
        return rows;
    }
}
